<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Setttings for component 'theme_rlmaster'
 *
 * @package   theme-rlmaster
 * @copyright 2011 Remote Learner  http://www.remote-learner.net/
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once(dirname(__FILE__).'/defaults.php');
require_once(dirname(__FILE__).'/admin_settings.php');

defined('MOODLE_INTERNAL') || die;

$context = get_context_instance(CONTEXT_SYSTEM); // Get context.
$filters = filter_get_active_in_context($context); // Get active filters.
$reinactive = array_key_exists('rein', $filters); // Get rein active or inactive.

if ($ADMIN->fulltree) {

    $themename = 'theme_'. substr($settings->name, 12);

    $name = $themename .'/tabs';
    $tabs = new rlmaster_admin_setting_tabs($name, $settings->name, $reload);
    $tabs->addtab(THEME_RLMASTER_TAB_CONTROLS, get_string('controls', $themename));
    $tabs->addtab(THEME_RLMASTER_TAB_COLORS, get_string('colors', $themename));
    $tabs->addtab(THEME_RLMASTER_TAB_CUSTOM, get_string('custom', $themename));
    $tabs->addtab(THEME_RLMASTER_TAB_IMAGES, get_string('images', $themename));
    $tabs->addtab(THEME_RLMASTER_TAB_LAYOUT, get_string('layout', $themename));
    // If the Rein.js filter is enabled, add the tab for its settings.
    $filters = filter_get_active_in_context($context);
    $reinactive = array_key_exists('rein', $filters);
    if ($reinactive) {
        $tabs->addtab(THEME_RLMASTER_TAB_REIN, get_string('rein', $themename));
    }
    $tabs->addtab(THEME_RLMASTER_TAB_MISC, get_string('misc', $themename));
    $settings->add($tabs);

    $tab = $tabs->get_setting();

    if ($tab === THEME_RLMASTER_TAB_CONTROLS) {
        // Control Section Heading
        $name = $themename .'/control_heading';

        $title = get_string('themecontrols', $themename);
        $link_name = get_string('rlmasterdocsname', 'theme_rlmaster');

        $url = 'http://rlcommunity.remote-learner.net/mod/book/view.php';
        $params = array('id' => 73);
        $link  = new moodle_url($url, $params);

        $attr = array('target' => '_blank');
        $description = html_writer::link($link, $link_name, $attr);
        $description = html_writer::tag('center', $description);

        $setting = new admin_setting_heading($name, $title, $description);
        $settings->add($setting);

        // Theme controls - must be last for import to work
        $name = $themename .'/controls';
        $title = get_string('themecontrols', $themename);
        $description = get_string('themecontrolsdesc', $themename);
        $setting = new rlmaster_admin_setting_controls($name, $title, $description);
        // Force page refresh on change - this only needs to be used once
        $setting->set_updatedcallback('rlmaster_admin_onsave_refresh');
        $settings->add($setting);

        // File uploader
        $name = $themename .'/file_upload';
        $title = get_string('fileupload', $themename);
        $description = get_string('fileuploaddesc', $themename);
        $setting = new rlmaster_admin_setting_fileupload($name, $title, $description);
        $settings->add($setting);

    } else if ($tab === THEME_RLMASTER_TAB_COLORS) {

        // Color array
        $name = $themename.'/colors';
        $title = get_string('colors', $themename);
        $description = get_string('colorsdesc', $themename);
        $setting = new rlmaster_admin_setting_colorarray($name, $title, $description);
        $settings->add($setting);

        // Clear theme cache on save
        $name = $themename .'/clearthemecache';
        $setting = new rlmaster_admin_setting_clearthemecache($name,'clearthemecache','Clear Theme Cache','');
        $settings->add($setting);

    } else if ($tab === THEME_RLMASTER_TAB_IMAGES) {

        foreach (rlmaster_admin_setting_controls::$images as $image) {
            $name = $themename .'/'. $image;
            $title = get_string($image, $themename);
            $description = get_string($image .'desc', $themename);

            switch ($image) {
                case 'favicon':
                    $setting = new rlmaster_admin_setting_faviconpicker($name, $title, $description, '');
                    break;
                default:
                    $setting = new rlmaster_admin_setting_filepicker($name, $title, $description, '');
                    break;
            }

            $settings->add($setting);
        }

        //Clear theme cache on save
        $name = $themename .'/clearthemecache';
        $setting = new rlmaster_admin_setting_clearthemecache($name,'clearthemecache','Clear Theme Cache','');
        $settings->add($setting);

    } else if ($tab === THEME_RLMASTER_TAB_CUSTOM) {

        // Custom CSS file
        $name = $themename .'/customcss';
        $title = get_string('customcss', $themename);
        $description = get_string('customcssdesc', $themename);
        $setting = new admin_setting_configtextarea($name, $title, $description, '');
        $settings->add($setting);

        // Custom head html
        $name = $themename .'/headcustomhtml';
        $title = get_string('headcustomhtml', $themename);
        $description = get_string('headcustomhtmldesc', $themename);
        $setting = new admin_setting_configtextarea($name, $title, $description, '');
        $settings->add($setting);

        // Custom header html
        $name = $themename .'/headercustomhtml';
        $title = get_string('headercustomhtml', $themename);
        $description = get_string('headercustomhtmldesc', $themename);
        $setting = new admin_setting_confightmleditor($name, $title, $description, '');
        $settings->add($setting);

        // Custom footer html
        $name = $themename .'/footercustomhtml';
        $title = get_string('footercustomhtml', $themename);
        $description = get_string('footercustomhtmldesc', $themename);
        $setting = new admin_setting_confightmleditor($name, $title, $description, '');
        $settings->add($setting);

        //Clear theme cache on save
        $name = $themename .'/clearthemecache';
        $setting = new rlmaster_admin_setting_clearthemecache($name,'clearthemecache','Clear Theme Cache','');
        $settings->add($setting);

    } else if ($tab === THEME_RLMASTER_TAB_LAYOUT) {

        // Layout type
        $name = $themename .'/layouttype';
        $title = get_string('layouttype', $themename);
        $description = get_string('layouttypedesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_LAYOUT_TYPE;
        $choices = array('fluid' => get_string('fluidwidth', $themename),
                         'fixed' => get_string('fixedwidth', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'layouttype', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Fluid width
        $name = $themename .'/fluidwidth';
        $title = get_string('fluidwidth', $themename);
        $description = get_string('fluidwidthdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_LAYOUT_FLUID;
        $choices = array('100%' => '100%', '95%' => '95%', '90%' => '90%', '85%' => '85%', '80%' => '80%');
        $checknotnull = rlmaster_check_not_null($themename, 'fluidwidth', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Fixed width
        $name = $themename .'/fixedwidth';
        $title = get_string('fixedwidth', $themename);
        $description = get_string('fixedwidthdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_LAYOUT_FIXED;
        $checknotnull = rlmaster_check_not_null($themename, 'fixedwidth', $default);
        $setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUM);
        $settings->add($setting);

        // Choose number of columns
        $name = $themename .'/columns';
        $title = get_string('columnsnumber', $themename);
        $description = get_string('columnsnumberdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_COL_NUM;
        $choices = array(2 => get_string('two', $themename),
                         3 => get_string('three', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'columns', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Column 1 Alignment
        $name = $themename .'/col1align';
        $title = get_string('col1align', $themename);
        $description = get_string('col1aligndesc', $themename);
        $default = 'left';
        $choices = array('left'  => get_string('left', $themename),
                         'right' => get_string('right', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'col1align', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Column 2 Alignment
        $name = $themename .'/col2align';
        $title = get_string('col2align', $themename);
        $description = get_string('col2aligndesc', $themename);
        $default = 'right';
        $choices = array('left'  => get_string('left', $themename),
                         'right' => get_string('right', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'col2align', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Block region width
        $name = $themename .'/regionwidth';
        $title = get_string('regionwidth', $themename);
        $description = get_string('regionwidthdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_COL_WIDTH;
        $choices = array('200px' => '200px', '210px' => '210px', '220px' => '220px', '240px' => '240px', '250px' => '250px');
        $regionwidthvalue = rlmaster_set_sane_value($themename, 'regionwidth', $choices);
        $checknotnull = rlmaster_check_not_null($themename, 'regionwidth', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Header height
        $name = $themename .'/headerheight';
        $title = get_string('headerheight', $themename);
        $description = get_string('headerheightdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_HEADER_HEIGHT;
        $checknotnull = rlmaster_check_not_null($themename, 'headerheight', $default);
        $setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUM);
        $settings->add($setting);

        // Banner height
        $name = $themename .'/bannerheight';
        $title = get_string('bannerheight', $themename);
        $description = get_string('bannerheightdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_BANNER_HEIGHT;
        $checknotnull = rlmaster_check_not_null($themename, 'bannerheight', $default);
        $setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUM);
        $settings->add($setting);

        //Clear theme cache on save
        $name = $themename .'/clearthemecache';
        $setting = new rlmaster_admin_setting_clearthemecache($name,'clearthemecache','Clear Theme Cache','');
        $settings->add($setting);

    } else if ($tab === THEME_RLMASTER_TAB_REIN) {

        // Link colour setting
        $name = $themename.'/reindefaultlight';
        $title = get_string('reindefaultlightlabel', $themename);
        $description = get_string('reindefaultlightdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_COLOR_REIN_DEFAULT_LIGHT;
        $checknotnull = rlmaster_check_not_null($themename, 'reindefaultlight', $default);
        $previewconfig = array('selector'=>'.block', 'style'=>'border-color');
        $setting = new admin_setting_configcolourpicker($name, $title, $description, $default, $previewconfig);
        $setting->set_updatedcallback('theme_reset_all_caches');
        $settings->add($setting);

        // Default medium color setting
        $name = $themename.'/reindefaultmedium';
        $title = get_string('reindefaultmediumlabel', $themename);
        $description = get_string('reindefaultmediumdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_COLOR_REIN_DEFAULT_MEDIUM;
        $previewconfig = array('selector'=>'.block', 'style'=>'color');
        $setting = new admin_setting_configcolourpicker($name, $title, $description, $default, $previewconfig);
        $setting->set_updatedcallback('theme_reset_all_caches');
        $settings->add($setting);

        // Default dark color setting
        $name = $themename.'/reindefaultdark';
        $title = get_string('reindefaultdarklabel', $themename);
        $description = get_string('reindefaultdarkdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_COLOR_REIN_DEFAULT_DARK;
        $previewconfig = array('selector'=>'.block .content', 'style'=>'background-color');
        $setting = new admin_setting_configcolourpicker($name, $title, $description, $default, $previewconfig);
        $setting->set_updatedcallback('theme_reset_all_caches');
        $settings->add($setting);

        // Active light color setting
        $name = $themename.'/reinactivelight';
        $title = get_string('reinactivelightlabel', $themename);
        $description = get_string('reinactivelightdesc', $themename);
        $active = THEME_RLMASTER_DEFAULT_COLOR_REIN_ACTIVE_LIGHT;
        $previewconfig = array('selector'=>'.block .content', 'style'=>'border-color');
        $setting = new admin_setting_configcolourpicker($name, $title, $description, $active, $previewconfig);
        $setting->set_updatedcallback('theme_reset_all_caches');
        $settings->add($setting);

        // Active medium color setting
        $name = $themename.'/reinactivemedium';
        $title = get_string('reinactivemediumlabel', $themename);
        $description = get_string('reinactivemediumdesc', $themename);
        $active = THEME_RLMASTER_DEFAULT_COLOR_REIN_ACTIVE_MEDIUM;
        $previewconfig = array('selector'=>'.block .content', 'style'=>'color');
        $setting = new admin_setting_configcolourpicker($name, $title, $description, $active, $previewconfig);
        $setting->set_updatedcallback('theme_reset_all_caches');
        $settings->add($setting);

        // Active dark color setting
        $name = $themename.'/reinactivedark';
        $title = get_string('reinactivedarklabel', $themename);
        $description = get_string('reinactivedarkdesc', $themename);
        $active = THEME_RLMASTER_DEFAULT_COLOR_REIN_ACTIVE_DARK;
        $previewconfig = array('selector'=>'.block .content', 'style'=>'background-color');
        $setting = new admin_setting_configcolourpicker($name, $title, $description, $active, $previewconfig);
        $setting->set_updatedcallback('theme_reset_all_caches');
        $settings->add($setting);

        //Clear theme cache on save
        $name = $themename .'/clearthemecache';
        $setting = new rlmaster_admin_setting_clearthemecache($name,'clearthemecache','Clear Theme Cache','');
        $settings->add($setting);

    } else {

        // Contexts in which custommenu appears
        $name = $themename .'/custommenu';
        $title = get_string('custommenu', $themename);
        $description = get_string('custommenudesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_CUSTOM_MENU;
        $choices = array('no'    => get_string('none'),
                         'front' => get_string('frontpage', $themename),
                         'all'   => get_string('allpages', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'custommenu', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Layout type
        $name = $themename.'/custommenuposition';
        $title = get_string('custommenuposition', $themename);
        $description = get_string('custommenupositiondesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_CUSTOM_MENU_POSITION;
        $choices = array('top' => get_string('custommenupositionabove', $themename),
                         'bottom' => get_string('custommenupositionbelow', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'custommenuposition', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Login info position
        $name = $themename .'/logintop';
        $title = get_string('logintop', $themename);
        $description = get_string('logintopdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_LOGIN_TOP;
        $checknotnull = rlmaster_check_not_null($themename, 'logintop', $default);
        $setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUM);
        $settings->add($setting);

        // Header rounded corners
        $name = $themename .'/headercorner';
        $title = get_string('headercorner', $themename);
        $description = get_string('headercornerdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_HEADER_CORNER;
        $choices = array('0px' =>'0px', '3px' => '3px', '5px' => '5px', '10px' => '10px', '15px' => '15px', '20px' => '20px', '30px' => '30px');
        $headercornervalue = rlmaster_set_sane_value($themename, 'headercorner', $choices);
        $checknotnull = rlmaster_check_not_null($themename, 'headercorner', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Heading height
        $name = $themename .'/headingheight';
        $title = get_string('headingheight', $themename);
        $description = get_string('headingheightdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_HEADING_HEIGHT;
        $checknotnull = rlmaster_check_not_null($themename, 'headingheight', $default);
        $setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUM);
        $settings->add($setting);

        // Block rounded corners
        $name = $themename .'/blockcorner';
        $title = get_string('blockcorner', $themename);
        $description = get_string('blockcornerdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_BLOCK_CORNER;
        $choices = array('0' =>'0', '3px' => '3px', '5px' => '5px', '10px' => '10px', '15px' => '15px');
        $blockcornervalue = rlmaster_set_sane_value($themename, 'blockcorner', $choices);
        $checknotnull = rlmaster_check_not_null($themename, 'blockcorner', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Block controls vertical alignment
        $name = $themename .'/blockcontrol';
        $title = get_string('blockcontrol', $themename);
        $description = get_string('blockcontroldesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_BLOCK_CONTROL;
        $choices = array('top'    => get_string('top'),
                         'middle' => get_string('middle', $themename),
                         'bottom' => get_string('bottom', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'blockcontrol', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Space between blocks
        $name = $themename .'/blockspace';
        $title = get_string('blockspace', $themename);
        $description = get_string('blockspacedesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_BLOCK_SPACE;
        $checknotnull = rlmaster_check_not_null($themename, 'blockspace', $default);
        $setting = new admin_setting_configtext($name, $title, $description, $default, PARAM_ALPHANUM);
        $settings->add($setting);

        // Tagline setting
        $name = $themename .'/tagline';
        $title = get_string('tagline', $themename);
        $description = get_string('taglinedesc', $themename);
        $setting = new admin_setting_configtext($name, $title, $description, '');
        $settings->add($setting);

        // Show course header bars
        $name = $themename .'/headingbar';
        $title = get_string('headingbar', $themename);
        $description = get_string('headingbardesc', $themename);
        $default = 'block';
        $choices = array('none' => get_string('hide'), 'block' => get_string('show'));
        $checknotnull = rlmaster_check_not_null($themename, 'headingbar', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        // Week Highlighting
        $name = $themename .'/weekhighlight';
        $title = get_string('weekhighlight', $themename);
        $description = get_string('weekhighlightdesc', $themename);
        $default = THEME_RLMASTER_DEFAULT_WEEK_HIGHLIGHT;
        $choices = array('full' => get_string('full', $themename),
                         'ends' => get_string('ends', $themename));
        $checknotnull = rlmaster_check_not_null($themename, 'weekhighlight', $default);
        $setting = new admin_setting_configselect($name, $title, $description, $default, $choices);
        $settings->add($setting);

        //Clear theme cache on save
        $name = $themename .'/clearthemecache';
        $setting = new rlmaster_admin_setting_clearthemecache($name,'clearthemecache','Clear Theme Cache','');
        $settings->add($setting);
    }
}
